<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base;

interface IForm
{
  public function getId();
  public function run();
  public function validate();
  public function getValidationStateClass($sField);
  public function getValidationMessage($sField);
  public function addCustomField($key, $value);
  public function getCustomField($key);
  public function __get($sField);
}

class Form extends \crcl\base\structure\Item implements IForm
{
  public function getId()
  {
    $sName = $this->getName();
    $sName = \ucfirst($sName);
    $sId = 'form' . $sName;

    return $sId;
  }

  public function run()
  {
    $sNamespace = $this->getId();
    if (isset($_POST[$sNamespace]))
    {
      return \TRUE;
    }
    return \FALSE;
  }

  public function validate()
  {
    $bReturn = \TRUE;
    $sNamespace = $this->getId();
    foreach ($_POST[$sNamespace] as $sField => $sValue)
    {
      try
      {
        $this->$sField->set($_POST[$sNamespace][$sField]);
      }
      catch (\Exception $e)
      {
        $bReturn = \FALSE;
      }
    }
    return $bReturn;
  }

  public function getValidationStateClass($sField)
  {
    if ($this->$sField->getLastError() !== \NULL)
    {
      return 'has-error';
    }
    return '';
  }

  public function getValidationMessage($sField)
  {
    $sMessage = '';
    $eValidation = $this->$sField->getLastError();
    if (!\is_null($eValidation))
    {
      switch ($eValidation->getCode())
      {
        case \crcl\base\validator\container\Exception::ERROR_MAXLENGTH_TOOLONG:
          $sMessage = 'Bitte Eingabe kürzen. Maximale Länge: ';
          $sMessage .= $eValidation->getCauses()->get('max') . ' Zeichen. ';
          $sMessage .= 'Aktuell: ' . $eValidation->getCauses()->get('length') . ' Zeichen.';
          break;
          case \crcl\base\validator\container\Exception::ERROR_MINLENGTH_TOOSHORT:
          $sMessage = 'Eingabe zu kurz. Minimum: ';
          $sMessage .= $eValidation->getCauses()->get('min');
          $sMessage .= ' / Aktuell: ' . $eValidation->getCauses()->get('length') . ' Zeichen.';
          break;

        case \crcl\base\validator\container\Exception::ERROR_NOTEMPTY_EMPTY:
          $sMessage = 'Bitte Feld ausfüllen.';
          break;
        case \crcl\base\validator\container\Exception::ERROR_MAIL_INVALID:
          $sMessage = 'Ungültige E-Mail.';
          break;
        default:
          $sMessage = $eValidation->getCode();
      }
    }

    return $sMessage;
  }

  public function addCustomField($key, $value)
  {
    $oField = \crcl\base\structure\Field::getInstance('custom://');
    $oField->set($value);
    $this->aCustomFields[$key] = $oField;
  }

  public function getCustomField($key)
  {
    return $this->aCustomFields[$key];
  }

  public function __get($sField)
  {
    try
    {
      $oField = parent::__get($sField);
    }
    catch (\crcl\base\structure\Exception $e)
    {
      if (\array_key_exists($sField, $this->aCustomFields))
      {
        $oField = $this->aCustomFields[$sField];
      }
      else
      {
        throw $e;
      }
    }
    return $oField;
  }

  /** PRIVATE *************************************************************************************/

  private $aCustomFields = array();
}

